#!/usr/bin/python -S
"""
p4_test.py: Tests for p4 JSON Patterns
"""

__author__ = 'Andy Chu'


import os
from pprint import pprint
import sys

from pan.core import cmdapp
from pan.core import params
from pan.core import json
from pan.core import util
from pan.test import testy

if __name__ == '__main__':
  # for jsonpattern package
  sys.path.insert(
      0, os.path.join(os.path.dirname(__file__), '..', 'python'))
  # Root of the project
  sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))

from tests import test_main

import jsonpattern


class DosTest(testy.Test):

  LABELS = ['multilanguage']

  def testDir(self):
    s = test_main.TestData("dir-s.txt")

    library = {}

    def Add(name, pattern, desc=None):
      library[name] = jsonpattern.Pattern(
          pattern, patterns=library, name=name, desc=desc)

    p = r"""
        P| { [\d,]+ | comma-int}
        """
    Add('comma-int', p)

    # TODO: Parse AM/PM time better.  Convert to an integer or other standard
    # time format.
    p = r"""
        P| \n
       SP| {} Directory of { .* dir}\n
        P| \n
         | repeated entries
        P|   {:M/D/Y date} \s+ {:H:M time} \s* { AM|PM m} \s* 
         |   choice
        P|     <DIR> \s* { .* dirname}
         |   or
        P|     {:comma-int size} \s* { .* filename}
         |   end
        P|   \n
         | end

        P| \s* {:int num-files} {.space} File\(s\)
        P| \s* {:comma-int bytes} {.space} bytes\n
        """
    Add('dir-listing', p)

    p = r"""
        P| {.space}Volume.*\n
        P| {.space}Volume.*\n
         | repeated directories
        P|     {:dir-listing}
         | end

        P| \n
        P| \s*Total Files Listed:\s*\n

        P| \s* {:int total-files} {.space} File\(s\)\s*
        P| {:comma-int total-bytes} {.space} bytes\n
         
        P| \s* {:int total-dirs} {.space} Dir\(s\)\s* 
        P| {:comma-int total-bytes-free} {.space} bytes free\n
        """
    Add('dir', p)

    p = library['dir']

    print p.AsTree()

    result = p.match(s)
    pprint(result.data)
    print result.MatchLog()


if __name__ == '__main__':
  test_main.main(__import__(__name__), sys.argv[1:])
